<?
/**
 * 文章功能模块
 * 低层数据输出和数据库存取
 *  
 * @copyright Copyright (c) 2008 sdmblog (http://www.sdmblog.cn)
 * @author sdomain(sdomain@live.cn)
 * @version article.model.php Sat May 17 16:52:39 CST 2008 php $
 */
class model_article {
	/**
	 * 查询语句，每次查询时把对应的查询句语保存在此处，以便调试
	 *
	 * @var string
	 */
	var $queryString = '';
	/**
	 * 响应记录总行数,即此查询的总行数
	 *
	 * @var int
	 */
	var $rowCount = 0;
	/**
	 * 查询结果行数，如果不分页，则与rowCount应该是一样的
	 *
	 * @var int
	 */
	var $queryCount = 0;
	/**
	 * 获取指定文章信息
	 *
	 * @param int $aid 文章编号
	 * @return array 文章信息
	 */
	function getArticle($aid) {
		global $db,$tablepre,$noLoginRight;
		$userinfo = islogin();
		$right = empty($userinfo[4])?$noLoginRight:$userinfo[4];
		$readRight = substr($right,0,1);
		$sql = 'select a.title,a.content,c.name as cname,c.id as cid,a.adddate,a.editdate,a.author,a.edituser,a.submitip from '.$tablepre.'articles a,'.$tablepre.'categories c where c.id = a.category and a.id='.$aid.' and a.type<='.$readRight;
		$this->queryString = $sql;
		$db->query($sql);
		$this->rowCount = $db->numRows;
		$this->queryCount = $db->numRows;
		$article = $db->db_fetch_array();
		return $article;
	}
	/**
	 * 获取指定文章的回复信息
	 *
	 * @param int $aid 文章编号
	 * @return array 文章回复LIST
	 */
	function getArticleComment($aid){
		global $db,$tablepre;
		//$sql = 'select from '.$tablepre.'revert r,'.$tablepre.'users u where u.id=r.userid and u.id=r.edituser and r.articleid='.$aid;
		$sql = 'select r.id,r.content,r.adddate,r.revertip,ua.username as adduser,adddate,ua.id as adduserid,ue.username as edituser,editdate,ue.id as edituserid from ('.$tablepre.'revert r left join '.$tablepre.'users ua on ua.id=r.userid)left join '.$tablepre.'users ue on ue.id=r.edituser where r.articleid = '.$aid;
		$this->queryString = $sql;
		$db->query($sql);
		$this->rowCount = $db->numRows;
		$this->queryCount = $db->numRows;
		while ($arr = $db->db_fetch_array()) {
			$arr['htmlcontent'] = getHtmlContent($arr['content']);
			$comments[] = $arr;
		}
		return $comments;
	}
	/**
	 * 通过文章的回复数据，生成回复的HTML代码，填充到页面上
	 *
	 * @param array $comments 文章的回复数据
	 * @return string 能在页面上显示的HTML代码
	 */
	function getArticleCommentHtml($comments)
	{
		global $lang,$virdir;
		if ($this->rowCount>0) {
			//有回复
			foreach ($comments as $comment){
				$strHTML .= '
				<DIV class=commentbox id=comment'.$comment['id'].'><A name=comment'.$comment['id'].'></A> 
	              <DIV class=commentbox-title>
				  	<A href="javascript:doQuote(\'comm_'.$comment['id'].'\',\''.$comment['adduser'].'\')"><IMG alt=Quote src="/'.$virdir.'styles/default/images/icon_quote.gif"></A> 
	                <B><A href="/'.$virdir.'user.php/act/view/id/'.$comment['adduserid'].'">'.$comment['adduser'].'</A></B> 
	                <DIV class=commentbox-label>
					  [ '.date('Y-m-d H:i:s',$comment['adddate']).' | '.$comment['$revertip'].' ]  <A title=Edit href="/'.$virdir.'comment.php/act/edit/id/'.$comment['id'].'"><IMG alt=Edit src="/'.$virdir.'styles/default/images/icon_edit.gif"></A> 
	                   <A title=Delete onclick="return confirm(\'Are you sure to delete this comment??\')" href="/'.$virdir.'comment.php/act/delete/id/'.$comment['id'].'"><IMG alt=Delete src="/'.$virdir.'styles/default/images/icon_del.gif"></A> 
	                </DIV>
	              </DIV>
	              <DIV class=commentbox-content id=comm_'.$comment['id'].'>
				  	'.$comment['htmlcontent'].'
	              </DIV>
	       		</DIV>';
				}
			return $strHTML;
		}else {
			//无回复
			return '<DIV class=no-comment-box>'.$lang['NO_ANY_COMMENT'].'</DIV>';
		}
	}
	/**
	 * 分类列表的HTML内容
	 *
	 * @param array $categories 分类数据
	 * @return string 在页面上显示的分类的HTML代码
	 */
	function getCategoryHtml($categories)
	{
		global $lang,$virdir;
		if (!is_array($categories)) {
			$strHTML = '<li><a href="/'.$virdir.'index.php"><b>首页</b></a></li>
						<li>'.$lang['NOCATEGORY'].'</li>';
		}else {
			foreach ($categories as $category) {
				$strHTML .= '
							<li><a href="/'.$virdir.'index.php/C/'.$category['id'].'">'.$category['name'].'['.$category['articlenum'].']</a></li>';
			}
			$strHTML = '<li><a href="/'.$virdir.'index.php"><b>首页</b></a></li>'.$strHTML;
		}
		$strHTML = '<ul>
					'.$strHTML.'
					</ul>';
		return $strHTML;
	}
	/**
	 * 通过一月的文章列表，得到用与显示日历的HTML代码
	 *
	 * @param int $year 年份
	 * @param int $month 月份
	 * @param array $arrdata 文章列表
	 * @return string 显示日历的HTML代码
	 */
	function getCalendarHtml($year,$month,$arrdata)
	{
		global $virdir;
		$date = strtotime($year.'-'.$month.'-'.'1');
		//一号星期几
		//$dayoneweek = date('w',$date);
		$i = 0;
		$day = 1;//日，从一开始
		$allday = date('t',strtotime($year.'-'.$month));//总天数
		//得出此月所有日的文章情况，如果有文章就把对应天内容加上文章
		for ($i=1;$i<=$allday;$i++){
			if (is_array($arrdata[$i])) {
				foreach ($arrdata[$i] as $daydata){
					$strtitle = empty($strtitle)?'':$strtitle."\r\n";
					$strtitle =$strtitle.'-'.$daydata['title'];
				}
				$arrdaystr[$i]= '<a href ="/'.$virdir.'index.php/D/'.$year.'-'.$month.'-'.$i.'" class="calendar" title="'.$strtitle.'">'.$i.'</a>';
				$strtitle ='';
			}else {
				//否则数据就是当天的日数
				$arrdaystr[$i] = $i;
			}
		}
		while ($day <= $allday) {
			for ($i=0;$i<7;$i++){
				$dayweek = date('w',strtotime($year.'-'.$month.'-'.$day));
				$nowyear = date('Y');
				$nowmonth = date('m');
				$nowday = date('d');
				if ((int)$nowyear==(int)$year &&(int)$nowmonth==(int)$month && (int)$nowday==(int)$day ) {
					$isnowdate = true;
				}else {
					$isnowdate = false;
				}
				if ($i==$dayweek && $day<=$allday) {
					if ($isnowdate) {
						$strtemp .='<td class="calendar-today">'.$arrdaystr[$day].'</td>'."\r\n";
					}else{
						if ($i==0) {
							$strtemp .='<td class="calendar-sunday">'.$arrdaystr[$day].'</td>'."\r\n";
						}elseif ($i==6){
							$strtemp .='<td class="calendar-saturday">'.$arrdaystr[$day].'</td>'."\r\n";
						}else {
							$strtemp .='<td class="calendar-day">'.$arrdaystr[$day].'</td>'."\r\n";
						}
					}
					$day++;
				}else {
					$strtemp .='<td class="calendar-day-blank"></td>'."\r\n";
				}
			}
			$strweek .='<tr>'."\r\n".$strtemp.'<tr>'."\r\n";
			$strtemp ='';
		}
		$preyearstr = ($year - 1).','.$month;
		$thisyearstr = $year.','.$month;
		$nextyearstr = ($year + 1).','.$month;
		$premonthstr = ($month==1?($year - 1):$year).','.($month==1?'12':($month -1));
		$thismonthstr = $thisyearstr;
		$nextmonthstr = ($month==12?($year + 1):$year).','.($month==12?'1':($month +1));
		$strdatestr = $year.'-'.$month;
		$thismonthex = date('M',strtotime($strdatestr));
		$strHTML ='
					<table cellspacing="1" width="100%" id="calendar">
					<tr>
					<td colspan="7" class="calendar-top">
					
						<a href="#" onclick="getCalendar('.$preyearstr.');">&laquo;</a>
						<a href="/'.$virdir.'index.php/D/'.$strdatestr.'"><span class="calendar-year">'.$year.'</span></a>
						<a href="#" onclick="getCalendar('.$nextyearstr.');">&raquo;</a>
						&nbsp;&nbsp;
						<a href="#" onclick="getCalendar('.$premonthstr.');">&laquo;</a>
						<a href="/'.$virdir.'index.php/D/'.$strdatestr.'"><span class="calendar-month">'.$thismonthex.'</span></a>
						<a href="#" onclick="getCalendar('.$nextmonthstr.');">&raquo;</a>
					
					</td>
					
					</tr>
					<tr class="calendar-weekdays">
						<td class="calendar-weekday-cell">Su</td>
						<td class="calendar-weekday-cell">Mo</td>
						<td class="calendar-weekday-cell">Tu</td>
						<td class="calendar-weekday-cell">We</td>
						<td class="calendar-weekday-cell">Th</td>
						<td class="calendar-weekday-cell">Fr</td>
						<td class="calendar-weekday-cell">Sa</td>
					</tr>
					'.$strweek.'
					</table>
					';
		return $strHTML;
	}
	/**
	 * 文章阅读计数
	 *
	 * @param int $id 文章编号
	 */
	function addViews($id)
	{
		global $db,$tablepre;
		$sql = 'update '.$tablepre.'articles set `views` = `views` + 1 where id ='.$id;
		$this->queryString = 'sql:'.$sql;
		$db->query($sql);
	}
	/**
	 * 文章数据入库
	 *
	 * @param array $article 保存文章数据的数组
	 * @return int 新保存的文章编号
	 */
	function saveArticle($article)
	{
		global $db,$tablepre;
		$now = time();
		$sql = 'insert into '.$tablepre.'articles(
				`title`,`content`,`category`,`adddate`,`editdate`,
				`author`,`edituser`,`submitip`,`type`) values(
				"'.$article['log_title'].'",
				"'.$article['message'].'",
				"'.$article['log_catid'].'",
				"'.$now.'",
				"0",
				"'.$article['author'].'",
				"",
				"'.$article['submitip'].'",
				"'.$article['log_mode'].'"
				)';
		$db->query($sql);
		$articleid = $db->db_insert_id();
		$db->query('update '.$tablepre.'categories set articlenum = articlenum + 1 where id='.$article['log_catid']);
		return $articleid;
	}
	/**
	 * 添加回复
	 *
	 * @param array $commInfo 回复信息表单数组
	 * @return int 回复的编号
	 */
	function addComment($commInfo)
	{
		global $db,$tablepre;
		$now = time();
		$sql = 'INSERT INTO `'.$tablepre.'revert` (
`id` ,`articleid` ,`userid` ,`content` ,`adddate` ,`revertip` )
VALUES (NULL , \''.$commInfo['articleid'].'\', \''.$commInfo['author'].'\', \''.$commInfo['message'].'\',\''.$now.'\', \''.$commInfo['submitip'].'\')';
		//throw new Exception($sql);
		$db->query($sql);
		$commid = $db->db_insert_id();
		$db->query('update '.$tablepre.'articles set comments = comments + 1 where id ='.$commInfo['articleid']);
		return $commid;
	}
}
/*
include_once('../include/ajax.common.php');
$article = new model_article();
var_dump($article->addComment(1));
*/
?>